package com.alibaba.sample.kantiezi.web.store.module.screen;

import com.alibaba.biz.command.Command;
import com.alibaba.biz.command.CommandSupport;
import com.alibaba.biz.command.dispatcher.CommandDispatcher;
import com.alibaba.biz.command.result.Result;

import com.alibaba.sample.kantiezi.dal.dataobject.Cart;
import com.alibaba.sample.kantiezi.web.common.WebConstant;

import com.alibaba.service.template.TemplateContext;

import com.alibaba.turbine.module.screen.TemplateScreen;
import com.alibaba.turbine.service.rundata.RunData;

import com.alibaba.webx.WebxException;

public abstract class ViewCart extends TemplateScreen {
    protected abstract CommandDispatcher getCommandDispatcher();

    protected void execute(RunData rundata, TemplateContext context)
            throws WebxException {
        Cart cart = (Cart) rundata.getSession().getAttribute(WebConstant.kantiezi_CART_KEY);

        if (cart == null) {
            cart = new Cart();
        }

        Command command = new CommandSupport("viewCart");

        command.getParameters().put("cart", cart);

        Result result = getCommandDispatcher().execute(command);

        if (result.isSuccess()) {
            cart = (Cart) result.getDefaultModel();

            context.put("cart", cart);
        }
    }
}
